package sspersonserver;

import sspersonserver.protocol.Protocol;

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Double
 * Date: 15.01.12
 * Time: 20:55
 * To change this template use File | Settings | File Templates.
 */
public class Client implements Runnable,IClient
{
	private Socket s;
	private InputStream is;
	private OutputStream os;
	private BufferedReader in = null;
	private PrintWriter out;
	private LoginData loginData;
	private boolean flag=true;
	private int authState;
	private int curPos=0;
	private int amountPerPage=3;


	public Client()
	{

	}

	public Client(Socket accept) {
		s = accept;
		try {
			os = s.getOutputStream();
			is = s.getInputStream();
			in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			out = new PrintWriter(os,true);
			//s.setKeepAlive(true);
		} catch (IOException e) {
			e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		}
		System.out.println("Client created"+s.toString());
		Logger.getLogger("PersonServer").log(Level.SEVERE,"Client thread started: "+s.toString());
	}

	public void sendMsg(String s)
	{
		/*if(loginData.login!=null)
		{
		System.out.println("To "+loginData.getLogin()+": "+s);
		Logger.getLogger("PersonServer").log(Level.SEVERE,"To "+loginData.getLogin()+": "+s);
		}
		else
		{
			System.out.println("To "+this+": "+s);
			Logger.getLogger("PersonServer").log(Level.SEVERE,"To "+this+": "+s);
		}    */

		try
		{
			os.write(s.getBytes());
		} catch (IOException e)
		{
			//e.printStackTrace();
			Logger.getLogger("PersonServer").log(Level.SEVERE,loginData.getLogin()+":"+e.getMessage());

		}catch (NullPointerException e)
		{
			//e.printStackTrace();
			Logger.getLogger("PersonServer").log(Level.SEVERE,loginData.getLogin()+":"+e.getMessage());
		}
	}
	public void disconnect(){
		try
		{

			in.close();
			out.close();
			is.close();
			os.close();
			s.close();

		} catch (IOException e)
		{
			e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		}
		
		Server.getClientProcessor().getClients().remove(this);
		System.out.println("Client disconnected.");
		Logger.getLogger("PersonServer").log(Level.SEVERE,"Client disconnected: "+loginData.getLogin());

	}

	public boolean isFlag()
	{
		return flag;
	}

	public void setFlag(boolean flag)
	{
		this.flag = flag;
	}

	@Override
	public void run() {
		/*if(s.isConnected())
		{

			String fromClient=null;
			try {
				while (flag) {
					//Отображаем полученное сообщение
					if(hasMessage())
					{
						fromClient = getMessage();
						System.out.println(loginData.getLogin()+": " + fromClient);
						Server.getProtocol().decodeSystemPacket(fromClient, this, Server.getClientProcessor().getClients());
						Logger.getLogger("TicTacToeServer").log(Level.SEVERE,loginData.getLogin()+": "+fromClient);
					}

				}
				//disconnect();
			}
			catch (NullPointerException e)
			{
				e.printStackTrace();

			}
		}  */
	}

	public boolean hasMessage()
	{

		boolean ret = false;

		//System.out.println(in.ready());
		try
		{
			if(in.ready())
			{
				ret = true;
			}
			else
			{
				ret = false;
			}
		} catch (IOException e)
		{
			e.printStackTrace();
			Logger.getLogger("PersonServer").log(Level.SEVERE,loginData.getLogin()+":"+e.getMessage());
		}
		if(disconnected())
		{
				disconnect();
		}
		return ret;
	}
	public String getMessage()
	{
		String ret=null;
		if(!disconnected()){


			try
			{
				ret = in.readLine();
				//Logger.getLogger("PersonServer").log(Level.SEVERE,loginData.getLogin()+": "+ret);
			} catch (IOException e)
			{
				e.printStackTrace();
				Logger.getLogger("PersonServer").log(Level.SEVERE,loginData.getLogin()+":"+e.getMessage());

				disconnect();

			}
		}

		return ret;
	}
	public boolean disconnected()
	{
		return s.isClosed();
	}

	public void setAuthState(int authState)
	{
		this.authState = authState;
	}

	public int getAuthState()
	{
		return authState;
	}

	public LoginData getLoginData()
	{
		return loginData;
	}

	public void setLoginData(LoginData loginData)
	{
		this.loginData = loginData;
	}

	public int getCurPos()
	{
		return curPos;
	}

	public void setCurPos(int curPos)
	{
		this.curPos = curPos;
	}

	public int getAmountPerPage()
	{
		return amountPerPage;
	}

	public void setAmountPerPage(int amountPerPage)
	{
		this.amountPerPage = amountPerPage;
	}
}
